package com.zzx.boot.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzx.boot.pojo.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 将这个元数据对象处理器
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 当TableFiled注解设置的fill是插入的时候执行该语句
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
        log.info(metaObject.toString());
    }
    /**
     * 当@TableFiled是update的时候执行该语句
     * @param metaObject
     */
    //类似于功能增强 如果执行更新操作就会执行该方法 参数就是传入的对象
    @Override
    public void updateFill(MetaObject metaObject) {

      metaObject.setValue("updateTime", LocalDateTime.now());
      metaObject.setValue("updateUser",BaseContext.getCurrentId());
        log.info(metaObject.toString());
    }
}
